package com.atilim.cargo.business;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.Query;

import com.atilim.cargo.model.Cargo;
import com.atilim.cargo.model.CargoHistory;


@Stateless
public class CargoManagementBean extends BaseBean {

	@Inject //ejb nin standartı validation bean enterprise java bean olarak işaretliyoruz 

	ValidationBean validationBean;
	
	public List<Cargo> searchCargo (Map<String, Object> params) {
		List<Cargo> result = this.find(Cargo.class, params);
		return result;
	}
	
	public void insertCargo (Cargo cargo)  {
		if (!validationBean.validateCargo(cargo)) {
			throw new RuntimeException("Kargo bilgileri geçerli değil");
		}
		this.insertObject(cargo);
		CargoHistory history = new CargoHistory();
		history.setCargoId(cargo.getId());
		history.setOperation(cargo.getStatus());
		history.setOperationTime(new Date());
		this.insertObject(history);
	}

	public void updateCargo (Cargo cargo) throws Exception {
		if (!validationBean.validateCargo(cargo)) {
			throw new RuntimeException("Kargo bilgileri geçerli değil");
		}
		this.updateObject(cargo);
	}
	
	public void deleteCargo (Cargo cargo) throws Exception {
		if (!validationBean.validateCargo(cargo)) {
			throw new RuntimeException("Kargo araca yüklendi kargo silinemez");
		}
		
		this.deleteObject(cargo);
	}
	
	public Cargo getCargo (long id) throws Exception {
		return this.getObject(Cargo.class, id);
	}
	
	//kargoyu araca ekler
	public void addCargoIntoVechile (long cargoId, long vechileId) {
		Query query = this.em.createQuery("update Cargo " +
				"set vechileId = :vechileId, " +
				"status = :status " +
				"where id = :cargoId");
		query.setParameter("cargoId", cargoId);
		query.setParameter("vechileId", vechileId);
		query.setParameter("status", Cargo.DURUM_YUKLENDI);
		query.executeUpdate();
		
		CargoHistory history = new CargoHistory();
		history.setCargoId(cargoId);
		history.setOperation(Cargo.DURUM_YUKLENDI);
		history.setOperationTime(new Date());
		this.insertObject(history);
	}
	
	public void addCargoIntoVechile (long [] cargoIds, long vechileId) {
		for (long cargoId : cargoIds) {
			addCargoIntoVechile(cargoId, vechileId);
		}
	}
	
	//Araçtan cargoyu çıkarır
	public void removeCargoFromVechile (long cargoId) {
		Query query = this.em.createQuery("update Cargo set status = :status where id = :cargoId");
		query.setParameter("cargoId", cargoId);
		query.setParameter("status", Cargo.DURUM_YUKLENMEDI); //kargo araçtan indirildi
		query.executeUpdate();
		
		CargoHistory history = new CargoHistory();
		history.setCargoId(cargoId);
		history.setOperation(Cargo.DURUM_YUKLENMEDI);
		history.setOperationTime(new Date());
		this.insertObject(history);

	}

	public void deliverCargo (long cargoId) {
		Query query = this.em.createQuery("update Cargo set status = :status where id = :cargoId");
		query.setParameter("cargoId", cargoId);
		query.setParameter("status", Cargo.DURUM_HEDEFTE); //kargo müşteride
		query.executeUpdate();
		
		CargoHistory history = new CargoHistory();
		history.setCargoId(cargoId);
		history.setOperation(Cargo.DURUM_HEDEFTE);
		history.setOperationTime(new Date());
		this.insertObject(history);

	}
	
	public void removeCargoFromVechile (long [] cargoIds) {
		for (long cargoId : cargoIds) {
			removeCargoFromVechile(cargoId);
		}
	}
	
	public void deliverCargo (long [] cargoIds) {
		for (long cargoId : cargoIds) {
			deliverCargo(cargoId);
		}
	}
	
}
